<template>
  <div>
    <div class="ser_div">
      <el-row>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 产品 </el-col>
            <el-col :span="16">
              <el-select
                v-model="ser.product_id"
                filterable
                @change="product_id_change"
                placeholder="请选择产品"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in productList"
                  :key="index"
                  :label="item.product_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-select
            filterable
            v-model="ser.model_number_id"
            placeholder="请选择型号"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in ModelNumberList"
              :key="index"
              :label="item.model_number"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button
            @click="model_number_click"
            style="margin-left: 15px; background: #1890ff; color: aliceblue"
          >
            搜索</el-button
          >
          <el-button
            @click="add_model_number"
            style="background: #1890ff; color: aliceblue"
            >新增型号</el-button
          >
        </el-col>
      </el-row>
    </div>

    <el-table :data="dataList" stripe style="width: 100%" border>
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column label="产品名称">
        <template slot-scope="scope">
          <span style='display: block;'>{{scope.row.product[0]?scope.row.product[0]?.product_name:''}}</span>
        </template>
      </el-table-column>
      <el-table-column label="型号">
        <template slot-scope="scope">
          <span style="display: block">{{ scope.row.model_number }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img
            :src="uploadImgUrl + scope.row.file.file_url"
            @click="push_img(scope.row)"
            style="
              cursor: pointer;
              display: block;
              width: 60px;
              display: block;
              margin: 0 auto;
            "
            v-if="scope.row.file != null"
          />
          <img
            src="../../../src/assets/zwtp.jpg"
            style="width: 60px; display: block; margin: 0 auto"
            v-else
          />
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="set_product(scope.row)"
            type="text"
            icon="el-icon-edit"
            style="margin-right: 15px"
            >编辑</el-button
          >
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="是否删除这条数据？"
            @confirm="del_product(scope.row)"
          >
            <el-button slot="reference" type="text" icon="el-icon-delete">
              删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        @prev-click="prev"
        @next-click="next"
        :current-page.sync="pagination.page"
        :page-size="pagination.per_page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="isProduct"
      width="40%"
      :close-on-click-modal="false"
      @close="close_diolog"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="关联产品" prop="product_ids">
          <el-select
            v-model="ruleForm.product_ids"
            filterable
            multiple
            placeholder="请选择产品"
            style="width:50%;"
          >
            <el-option
              v-for="item in productList"
              :key="item.id"
              :label="item.product_name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="型号名称" prop="model_number">
          <el-input v-model="ruleForm.model_number" style="width:50%;"></el-input>
        </el-form-item>
        <el-form-item label="型号图片" prop="file_id">
          <uplodeImg
            ref="Upload"
            :value="imgUrl"
            :limit="limit"
            @subImg="subImg"
          ></uplodeImg>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="submitadd('ruleForm')"
          >确定</el-button
        >
      </span>
    </el-dialog>
    <bigimg
      :imgurl="bigurl"
      :product="bigproduct"
      :model_number="bigmodel_number"
      ref="img_big"
    ></bigimg>
  </div>
</template>

<script>
import {
  getModelNumberList,
  addModelNumber,
  delModelNumber,
  getProductList
} from "@/api/product/index";
import uplodeImg from "@/components/ImageUpload";
import bigimg from "@/components/bigimg";
export default {
  components: {
    uplodeImg,
    bigimg,
  },
  data() {
    return {
      ser: {
        product_id: "",
        model_number_id: "",
      },
      bigurl: "",
      bigproduct: "",
      bigmodel_number: "",
      uploadImgUrl: this.$global.imgQianJui,
      limit: 1,
      imgUrl: "",
      model_number: "",
      isprice: false,
      price: "",
      title: "",
      isProduct: false,
      dataList: [],
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      ruleForm: {
        id: "",
        model_number: "",
        product_ids: [],
        file_id: "",
      },
      rules: {
        product_ids: [
          { required: true, message: "请选择产品", trigger: "blur" },
        ],
        model_number: [
          { required: true, message: "请输入型号名称", trigger: "blur" },
        ]
      },
      specificationlist: [],
      productList: [],
      ModelNumberList: [],
      producttypelist: [],
    };
  },
  mounted() {
    this.getlist();
    this.getProduct();
  },
  methods: {
    product_id_change(val) {
      this.ModelNumberList = [];
      this.ser.model_number_id = "";
      this.get_producModel(val);
    },
    get_producModel(id) {
      getModelNumberList({ product_id: id,field_list:'id,model_number',is_dropdown:1 }).then((res) => {
        if (res.code == 200) {
          this.ModelNumberList = res.data.data;
        }
      });
    },
    push_img(row) {
      this.bigurl = row.file.file_url;
      this.bigproduct = row.product[0].product_name;
      this.bigmodel_number = row.model_number.model_number;
      this.$refs.img_big.open();
    },
    subImg(id) {
      this.ruleForm.file_id = id;
      console.log(id);
    },
    //获取产品
    getProduct() {
      getProductList().then((res) => {
        if (res.code == 200) {
          this.productList = res.data.data;
        }
      });
    },
    set_product(row) {
      console.log(row);
      if (row.file) {
        this.imgUrl = row.file.file_url;
      }
      this.ruleForm.id = row.model_number.id;
      this.ruleForm.model_number = row.model_number;
      this.ruleForm.product_ids.push(row.product[0].id)
      this.title = "编辑型号";
      this.isProduct = true;
    },
    add_model_number() {
      this.title = "新增型号";
      this.isProduct = true;
      this.imgUrl = "";
      this.ruleForm = {
        id: "",
        model_number: "",
        product_ids: [],
        file_id: "",
      };
      this.imgUrls = "";
      this.imgUrl = "";
      this.$nextTick(function () {
        this.$refs.Upload.pus_value();
      });
    },
    close_diolog() {
      this.ruleForm = {
        id: "",
        model_number: "",
        product_ids: [],
        file_id: "",
      };
    },
    submitadd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addModelNumber(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.getlist();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getlist() {
      getModelNumberList({ ...this.pagination, ...this.ser }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    model_number_click() {
      this.pagination = {
        per_page: 10,
        page: 1,
      };
      getModelNumberList({
        ...this.ser,
        ...this.pagination,
      }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
    del_product(row) {
      delModelNumber({
        id: row.id,
        is_del: 1,
      }).then((res) => {
        if (res.code == 200) {
          this.isProduct = false;
          this.$message.success("提交成功");
          this.getlist();
        }
      });
    }
  },
};
</script>

<style></style>
